package org.csu.corp.util;

import org.csource.common.MyException;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileUtil {
    private static String path = "D:\\ZlunTemp\\static\\uploadFile";

    public static String upload(MultipartFile multipartFile) throws IOException {
        if (!multipartFile.isEmpty()) {
            String fileName = multipartFile.getOriginalFilename();
            String fileType = fileName.substring(fileName.lastIndexOf("."));
            String curYearMonth = DateUtil.getCurrentYearMonthString();

            File autoFileDirectory = new File(path + "/" + curYearMonth);
            if (!autoFileDirectory.exists()) {
                autoFileDirectory.mkdirs();
            }
            String writeFileName = UUID.randomUUID().toString() + "-" + DateUtil.getCurrentTimeStamp().toString().substring(3) + fileType;
            File writeFile = new File(autoFileDirectory.getPath(), writeFileName);
            multipartFile.transferTo(writeFile);
            return autoFileDirectory.getPath() + writeFileName;
        }
        return null;
    }
}
